import java.util.Random;
import java.util.Scanner;

public class GuessNumber {

    public static void main(String[] args) {


        int realNumber = getRandDom();
        System.out.println(realNumber);

        // 将随机数放入数组中
        int temp1 = realNumber;
        int[] a = new int[4];
        for (int i = 3; i >= 0; i--) {
            a[i] = temp1 % 10;
            temp1 /= 10;
        }


        System.out.print("猜一猜4个不重复的数字，");
        // 输入
        Scanner sc = new Scanner(System.in);

        // 循环输入、判断
        while (true) {
            System.out.println("请输入4个不重复的数字：");
            int number = sc.nextInt();

            // 将输入的数字放入数组中
            int temp2 = number;
            int[] b = new int[4];
            for (int i = 3; i >= 0; i--) {
                b[i] = temp2 % 10;
                temp2 /= 10;
                System.out.println(b[i]);
            }

            // 判断数字和位置
            int countInt = 0;
            int countIndex = 0;
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (a[i] == b[j]){
                        countInt++;
                    }
                }
                if (a[i] == b[i]){
                    countIndex++;
                }
            }


            if (number == realNumber) {
                System.out.println("回答正确");
                break;
            }else{
                System.out.println("包含了" + countInt + "个正确的数字");
                System.out.println(countIndex + "个数字位置正确");
                System.out.println();
            }


        }


    }

    // 随机生成不重复数字的四位数
    public static int getRandDom(){
        String number = "";
        Random rd = new Random();
        while (number.length() != 4) {
            String rn = rd.nextInt(10) + "";
            if (number.length()==1){
                if ("0".equals(rn)){
                    continue;
                }
            }
            // 随机到和前面的数字不同的数在加进去
            if (!number.contains(rn)){
                number += rn;
            }
        }
        return Integer.parseInt(number);
    }

}
